package cn.com.darly.videoandroidpm.ui.presenter;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.darly.common.ToastApp;
import com.darly.common.retrofit.FrameListener;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseApplication;
import cn.com.darly.videoandroidpm.bean.BaseBean;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.EncryperAES;
import cn.com.darly.videoandroidpm.common.SystemLoginInfo;
import cn.com.darly.videoandroidpm.ui.RegisterActivity;
import cn.com.darly.videoandroidpm.ui.biz.LoginListener;


/**
 * @author zhangyh2 s 上午10:57:39 TODO 控制器 页面动作操作
 */
public class LoginPresenter {

    private LoginListener userBiz;
    private LoginListener.LoginInterface main;


    public LoginPresenter(LoginListener.LoginInterface main) {
        this.main = main;
        this.userBiz = new LoginListener();
    }


    /**
     * @param context
     * @param v       TODO点击事件在这里进行处理
     */
    public void onClickDown(Context context, View v) {
        switch (v.getId()) {
            case R.id.id_login_set:
                if (!ActivityManager.isUserAMonkey()) {
                    Intent intent = new Intent(context, RegisterActivity.class);
                    intent.putExtra("title",R.string.register_title);
                    context.startActivity(intent);
                }
                break;
            case R.id.id_login_btn:
                if (TextUtils.isEmpty(main.getUserName()) || TextUtils.isEmpty(main.getPassword())) {
                    ToastApp.showToast("用户名密码不为空");
                    main.setEnableClick();
                    return;
                }

                if (main.getPassword().contains(" ") && main.getUserName().contains(" ")) {
                    main.setEnableClick();
                    ToastApp.showToast("用户名密码不能包含空格");
                    return;
                }
                Pattern pattern = Pattern
                        .compile("([^\\._\\w\\u4e00-\\u9fa5])*");
                Matcher matcher = pattern.matcher(main.getUserName());
                if (matcher.matches()) {
                    ToastApp.showToast("用户名不能包含表情");
                    main.setEnableClick();
                    return;
                }

                SystemLoginInfo.setParamer("username",main.getUserName());
                SystemLoginInfo.setParamer("password",main.getPassword());
                String deviceId = BaseApplication.getInstance().getDevId();
                Map<String, String> params = new LinkedHashMap<String, String>();
                params.put("deviceid", deviceId);
                params.put("password", EncryperAES.Encode(main.getPassword()));
                params.put("username", main.getUserName());
                login(params);
                break;
            default:
                break;
        }
    }


    public void login(Map<String, String> params) {
        main.setDisableClick();
        userBiz.onLogin(params, new FrameListener<BaseBean<LoginInfo>>() {
            @Override
            public void onSucces(BaseBean<LoginInfo> info) {
                main.setEnableClick();
                main.onLoginSuccess(info.getData());
            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onLoginFailed(info);
                }
            }
        });
    }
}